package com.example.uit_management;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ac_re extends Activity {

	/**Mã sinh viên */
	static final String Student_ID =MainActivity.Student_ID; 
	
	// Variable of class
	
	/**Biến kiểu listView */
	private ListView list;
	/**Biến kiểu ArrayAdapterRegistry */
	private ArrayAdapterRegistry adapter;
	/**Biến kiểu JsonParser_course */
	private JsonParser_course JSON = new JsonParser_course();		
	/**Biến kiểu ArrayList<HashMap<String, String>> */
	private ArrayList<HashMap<String, String>> ArrayMap = new ArrayList<HashMap<String, String>>(); 
	/**Biến kiểu HashMap<String, String> */
	private HashMap<String, String> Map = new HashMap<String, String>();
	/**Biến kiểu ArrayList<HashMap<String, String>> */
	private ArrayList<HashMap<String, String>> ArrayMap_listener = new ArrayList<HashMap<String, String>>(); 
	/**Biến kiểu HashMap<String, String> */
	private HashMap<String, String> Map_listener = new HashMap<String, String>();

	// Các biến information
	private static final int NEW_MENU_ITEM = Menu.FIRST;
	private static final int IP_Menu = NEW_MENU_ITEM;
	private static int Size=0 ;
	
	
	@Override
    public void onCreate(Bundle savedInstanceState) {		
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_registry);     
        Menu_Activity.dialog.dismiss();
        ////////////////////////////
        //Show custom  listview
        //////////////////////////////
        // get Sting Map
        ArrayMap = JSON.getMap();
        // Get Id of list
        list=(ListView)findViewById(R.id.list_registry); 
        // call constructor method of adapter
        adapter =new ArrayAdapterRegistry(this, ArrayMap);
        // add adapter into list
        if(ArrayMap.size()==0){
        	Toast.makeText(this, "Không có môn nào", Toast.LENGTH_LONG).show();
        }
        /*for(int i=0; i<ArrayMap.size();i++){
        	Log.e(Activity_list_registry.class.toString(),ArrayMap.get(i).get(JSON.KEY_subjectName));
        }*/
        list.setAdapter(adapter);
        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				

			}
		});		       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       
    	 MenuInflater inflater = getMenuInflater();
    	    inflater.inflate(R.menu.activity_list_registry, menu);    
    	    try {
    	    	// Create a Submenu of oder menu
    	    	 //Log.e(Activity_list_registry.class.toString(), "first add sub menu");
        	    SubMenu sub = menu.findItem(R.id.order_list).getSubMenu();
        	    sub.clear();
        	    // Create a viable get Json form server
        	    //Log.e(Activity_list_registry.class.toString(), "Get data json sub add menu");
        	    JsonParser_Faculty Js_faculty = new JsonParser_Faculty();        	   
        	    ArrayMap = Js_faculty.getMap_Faculty();        	   
        	    // Get size of Array HashMap   	
        	    Size = ArrayMap.size();
        	    for(int i = 0; i < ArrayMap.size();i++){  
        	    		Map = ArrayMap.get(i);        	    		       	    		
        	    		//Log.e(Activity_list_registry.class.toString(), "add sub add menu" );
        	    	    sub.add(i+1,IP_Menu+i+1,i+1,Map.get(JsonParser_Faculty.KEY_facultyID)+"_"+Map.get(JsonParser_Faculty.KEY_facultyName));    	    	   
        	    }
        	    sub.add(0,IP_Menu,0,"All");
			} catch (Exception e) {
				// TODO: handle exception
				Log.e(Activity_list_registry.class.toString(), "Exception sub add menu");
			}
    	    
    	    return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.save_list:     //Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show();
            {
            	Save_Menu();
            	/*list = (ListView) findViewById(R.id.list_registry);
            	String data ="";
            	for(int i= 0; i< list.getCount() ; i++)
                {            		
                     View v = list.getChildAt(i);
                       try {
                                TextView tv = (TextView)  v.findViewById(R.id.subjectName);
                                CheckBox cb = (CheckBox)  v.findViewById(R.id.check);                               
                             
                                if(cb.isChecked()){
                                    //Log.e(Activity_list_registry.class.toString(), "Try Activity_list_registry :"+tv.getText().toString().split("_")[0]);
                                    //Json.PutDataRegCourse(tv.getText().toString().split("_")[0], Student_ID, "add");
                                    if(data.equals("")){
                                    	data+=tv.getText().toString().split("_")[0];
                                    }else
                                    	data+=","+tv.getText().toString().split("_")[0];
                                    //Log.e(Activity_list_registry.class.toString(), "Try Activity_list_registry finish add :"+tv.getText().toString().split("_")[0]);
                                }
                               }
                           catch(Exception e){
                        	   //Log.e(Activity_list_registry.class.toString(), "Exception Activity_list_registry");
                           }
                }     
            	//Log.e(Activity_list_registry.class.toString(), "Data send :"+data);
                JSON.PutDataRegCourse(data, Student_ID, "add");*/
            }                                  
            break;               
            case R.id.back_list: startActivity(new Intent(getApplicationContext(),Menu_Activity.class));
                                break;
        }
        // Listenter oder menu       
        for(int i=0; i<Size; i++){
        	// Choose all item menu
        	if(item.getItemId() == IP_Menu)        	{        		
        		//Log.e(Activity_list_registry.class.toString(), "Listen ID faculty all");
        		ArrayMap_listener = JSON.getMap();
            	if(ArrayMap_listener.size()==0){
                	Toast.makeText(this, "Không có môn nào", Toast.LENGTH_LONG).show();
                }            	
                 // Get Id of list
                 list=(ListView)findViewById(R.id.list_registry);
                 // call constructor method of adapter
                 adapter =new ArrayAdapterRegistry(this, ArrayMap_listener);
                 // add adapter into list
                 list.setAdapter(adapter);
                 break;
        	}else
        	// choose other item menu
        	if(item.getItemId() == IP_Menu+1+i){
        	Map_listener = ArrayMap.get(i);
        	//Log.e(Activity_list_registry.class.toString(), "Listen ID faculty"+hashMap.get(JsonParser_Faculty.KEY_facultyID));
        	ArrayMap_listener = JSON.getMapKey(Map_listener.get(JsonParser_Faculty.KEY_facultyID));
        	if(ArrayMap_listener.size()==0){
            	Toast.makeText(this, "Không có môn nào", Toast.LENGTH_LONG).show();
            } 
        	// Get Id of list
             list=(ListView)findViewById(R.id.list_registry);
             // call constructor method of adapter
             adapter =new ArrayAdapterRegistry(this, ArrayMap_listener);
             // add adapter into list
             list.setAdapter(adapter);     
             break;
        	}
        }
        return true;
    }
    //////////////////////
    // SAve method
    ////////////////////////
    public void Save_Menu(){
    	 AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(ac_re.this);
    	 myAlertDialog.setTitle("THÔNG BÁO");
    	 myAlertDialog.setMessage("BẠN MUỐN ĐĂNG KÍ ?");
    	 myAlertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {

    	  public void onClick(DialogInterface arg0, int arg1) {
    	  // do something when the OK button is clicked
    		Log.e(Activity_list_registry.class.toString(), "1");
    		list = (ListView) findViewById(R.id.list_registry);
          	String data ="";
          	Log.e(Activity_list_registry.class.toString(), "2");
          	//ArrayList<String> stringArrayList = new ArrayList<String>();
          	for(int i= 0; i< list.getCount() ; i++)
              {      
          			Log.e(Activity_list_registry.class.toString(),  ""+list.getCount()+"aaa"+i);
                   View v = list.getChildAt(i);
                   Log.e(Activity_list_registry.class.toString(),  ""+i);
                     try {
                    	 Log.e(Activity_list_registry.class.toString(), "4");
                              TextView tv = (TextView)  v.findViewById(R.id.subjectName);
                    			Log.e(Activity_list_registry.class.toString(),  ""+list.getCount()+"aaa"+i);

                              CheckBox cb = (CheckBox)  v.findViewById(R.id.check);  
                    			Log.e(Activity_list_registry.class.toString(),  ""+list.getCount()+"aaa"+i);

                              
                         	 Log.e(Activity_list_registry.class.toString(), ""+tv.getText().toString().split("_")[0]);

                              if(cb.isChecked()){
                            	  //stringArrayList.add(tv.getText().toString().split("_")[0].split(".")[0]);
                                  //Log.e(Activity_list_registry.class.toString(), "Try Activity_list_registry :"+tv.getText().toString().split("_")[0].split(".")[0]);
                                  //Json.PutDataRegCourse(tv.getText().toString().split("_")[0], Student_ID, "add");
                            	  Log.e(Activity_list_registry.class.toString(), "44");
                            	  if(data.equals("")){
                                  	data+=tv.getText().toString().split("_")[0];
                                  }else
                                  	data+=","+tv.getText().toString().split("_")[0];
                                  //Log.e(Activity_list_registry.class.toString(), "Try Activity_list_registry finish add :"+tv.getText().toString().split("_")[0]);
                              }
                              Log.e(Activity_list_registry.class.toString(), "5");
                             }
                         catch(Exception e){
                          e.printStackTrace();
                      	   Log.e(Activity_list_registry.class.toString(), "Exception Activity_list_registry");
                         }
              }      
       	   Log.e(Activity_list_registry.class.toString(), "data :"+data);

          	JSON.PutDataRegCourse(data, Student_ID, "add");
    	  }});
    	 myAlertDialog.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
    	       
    	  public void onClick(DialogInterface arg0, int arg1) {
    	  // do something when the Cancel button is clicked
    	  }});
    	 myAlertDialog.show();
    }
}

